// component/j-bespeak/j-bespeak.js
var app = getApp();
Component({
	/**
	 * 组件的属性列表
	 */
  properties: {

  },

	/**
	 * 组件的初始数据
	 */
  data: {
    curindex: -1,
    mask:false,
    dateIndex: 0,
    timeIndex: 0,
  },

  ready: function () {
    var that = this;


    var myDate = new Date();
    var arr = [];
    var Week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];


    for (var i = 0; i < 20; i++) {
      myDate.setDate(myDate.getDate() + 1)
      // arr.push(Week[myDate.getDay()] + ' ' + (myDate.getMonth() + 1) + '/' + myDate.getDate())
      arr.push({
        week: Week[myDate.getDay()],
        date: (myDate.getMonth() + 1) + '-' + myDate.getDate()
      })
    }

    var timeArr = []
    for (var j = 9; j <= 22; j++) {
      timeArr.push(j + ':00')
      if (j == 23) {
        break;
      }
      timeArr.push(j + ':30')
    }
    console.log(timeArr)
    that.setData({
      dateList: arr,
      timeList: timeArr,
      thisData: arr[0],
      thisTime: timeArr[0]
    })



    wx.request({
      url: app.globalData.host + 'index.php/api/goods/classify?admin_id=' + app.globalData.admin_id + '&parent_id=' + 67127,
      success: function (e) {
        console.log(e)
        let arr = []
        for (var i = 0; i < e.data.data.length; i++) {
          arr.push(e.data.data[i].name)
        }
        that.setData({
          pxarr: arr
        })
      }
    })
  },

	/**
	 * 组件的方法列表
	 */
  methods: {
    bindDateChange: function (e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        px: e.detail.value
      })
    },
    dateclick: function (e) {
      this.setData({
        dateIndex: e.currentTarget.dataset.index
      })

    },
    timeclick: function (e) {
      this.setData({
        timeIndex: e.currentTarget.dataset.index
      })
    },
    qd:function(){
      this.setData({
        mask:false,
        time: this.data.dateList[this.data.dateIndex].week + this.data.dateList[this.data.dateIndex].date  + '-' + this.data.timeList[this.data.timeIndex]
      })
    },
    close: function (e) {
      this.setData({
        mask: false
      })
    },
    show(){
      this.setData({
        mask:true
      })
    },
    submit: function (e) {
      console.log(e);
      var that = this;
      if (e.detail.value.name.length == 0) {
        wx.showToast({
          title: '姓名不能为空',
          image: '../../images/error.png',
          duration: 1000
        })
      }
      else if (!(/^1(3|4|6|5|7|8)\d{9}$/.test(e.detail.value.tell))) {
        wx.showToast({
          title: '电话格式有误',
          image: '../../images/error.png',
          duration: 1000
        })
      }
      else if (e.detail.value.time.length == 0) {
        wx.showToast({
          title: '时间不能为空',
          image: '../../images/error.png',
          duration: 1000
        })
      }
      else if (e.detail.value.px == -1) {
        wx.showToast({
          title: '项目不能为空',
          image: '../../images/error.png',
          duration: 1000
        })
      }
      else {
        wx.request({
          url: 'https://xcxu.we-fs.com/index.php/api/goods/order1',
          method: 'post',
          data: {
            'name': e.detail.value.name,
            'sj': e.detail.value.tell,
            // 'pre_time': e.detail.value.time,
            'dz': '备注:' + e.detail.value.p,
            'goods_name': '【预约】' + that.data.pxarr[e.detail.value.px] + '【预约时间】' + e.detail.value.time,
            'admin_id': app.globalData.admin_id,
            'openid': app.globalData.openid,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            console.log(res.data)
            
            that.setData({
              list: res.data[0],
            })
            wx.showToast({
              title: '预约成功',
              icon: 'success',
              duration: 2000,
              success: function () {
                
                setTimeout(function () {
                  wx.navigateTo({
                    url: '../../pages/success/success?time=' + that.data.time,
                  })
                  that.setData({
                    name: '',
                    tell: '',
                    p: '',
                    px: -1,
                    time: ''
                  })
                }, 1500)
              }
            })
            setTimeout(function () {
              that.setData({
                loadingHidden: true
              })
            }, 1500)
          },
          fail: function (res) {
            console.log(res);
            wx.showToast({
              title: '预约失败！',
              duration: 2000
            })
          }
        })
      }
    }
  }
})
